package com.jstyle.jclife.activity;

import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.jstyle.blesdk.cmdenum.SendCmdState;
import com.jstyle.blesdk.constant.DeviceKey;
import com.jstyle.blesdk.model.PersonalGoal;
import com.jstyle.jclife.R;
import com.jstyle.jclife.ble.BleManager;
import com.jstyle.jclife.fragment.HomePageFragment;
import com.jstyle.jclife.model.BleData;
import com.jstyle.jclife.utils.DateUtil;
import com.jstyle.jclife.utils.RxBus;
import com.jstyle.jclife.utils.ScreenUtils;
import com.jstyle.jclife.utils.SharedPreferenceUtils;
import com.jstyle.jclife.utils.Utils;
import com.jstyle.jclife.view.DecimalScaleRulerView;

import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class GoalActivity extends BaseActivity {

    @BindView(R.id.bt_goal_back)
    Button btGoalBack;
    @BindView(R.id.DecimalScaleRulerView_steps)
    DecimalScaleRulerView DecimalScaleRulerViewSteps;
    @BindView(R.id.DecimalScaleRulerView_sleep)
    DecimalScaleRulerView DecimalScaleRulerViewSleep;
    @BindView(R.id.bt_goal_confim)
    Button btGoalConfim;
    @BindView(R.id.tv_goal_steps)
    TextView tvGoalSteps;
    @BindView(R.id.tv_goal_sleepTime)
    TextView tvGoalSleepTime;
    int stepGoal=10000;
    float sleepGoal=480;
    private AlertDialog alertDialog;
    final  static int maxStepValue=50000;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_goal);
        ButterKnife.bind(this);
        init();
    }

    private void init() {
        ScreenUtils.setTitleTypeface(btGoalBack);
        sleepGoal=SharedPreferenceUtils.getFloat(SharedPreferenceUtils.KEY_Sleep_goal,480);
        stepGoal=SharedPreferenceUtils.getInteger(SharedPreferenceUtils.KEY_Step_goal,10000);
        tvGoalSleepTime.setText(DateUtil.getSleepTime((int) (sleepGoal)));
        tvGoalSteps.setText(String.valueOf(stepGoal));
        sendData(SendCmdState.GET_GOAL);
        DecimalScaleRulerViewSteps.setParam( 10,
                32, 24,
                14, 9,
                12);
        DecimalScaleRulerViewSteps.setValueChangeListener(new DecimalScaleRulerView.OnValueChangeListener() {
            @Override
            public void onValueChange(float value) {
                stepGoal= (int) value;
                tvGoalSteps.setText(String.valueOf(stepGoal));
            }
        });
        DecimalScaleRulerViewSteps.initViewParam(stepGoal, 2000, maxStepValue, 100);

        DecimalScaleRulerViewSleep.setValueChangeListener(new DecimalScaleRulerView.OnValueChangeListener() {
            @Override
            public void onValueChange(float value) {
                tvGoalSleepTime.setText(DateUtil.getSleepTime((int) (value*6)));
                sleepGoal=value*6;
            }
        });
        DecimalScaleRulerViewSleep.setParam( 10,
                32, 24,
                14, 9,
                12);
        DecimalScaleRulerViewSleep.initViewParam(sleepGoal/6, 40, 120, 1);
        DecimalScaleRulerViewSleep.setZoomLevel(0.1f);
    }

    @OnClick({R.id.bt_goal_back, R.id.iv_back,R.id.bt_goal_confim})
    public void onViewClicked(View view) {
        if(Utils.isFastClick()){return;}
        switch (view.getId()) {
            case R.id.iv_back:

                finish();
                break;
            case R.id.bt_goal_confim:
                setGoal();
                break;

        }
    }

    private void setGoal() {
        SharedPreferenceUtils.setSpFloat(SharedPreferenceUtils.KEY_Sleep_goal,sleepGoal);
        if(!BleManager.getInstance().isConnected()){
            showToast(getString(R.string.disonnected));
            return;
        }
        RxBus rxBus=RxBus.getInstance();
        BleData data=new BleData();
        data.setAction(HomePageFragment.ACTION_UPDATE_GOA);
        rxBus.post(data);
        SharedPreferenceUtils.setSpInteger(SharedPreferenceUtils.KEY_Step_goal,stepGoal);

        PersonalGoal personalGoal=new PersonalGoal();
        personalGoal.setStepGoal(stepGoal);
        sendData(SendCmdState.SET_GOAL,personalGoal);
//        if(alertDialog==null){
//            alertDialog=new AlertDialog.Builder(this,R.style.appalertdialog)
//                    .setMessage(getString(R.string.Successful))
//                    .setNegativeButton(getString(R.string.Confirm),null).create();
//        }
//        alertDialog.show();

      ScreenUtils.showSetSuccessFul(btGoalBack);
    }

    @Override
    public void dataCallback(Map<String, String> maps, SendCmdState sendCmdState) {
        super.dataCallback(maps, sendCmdState);
        switch (sendCmdState){
            case GET_GOAL:
                stepGoal= Integer.parseInt(maps.get(DeviceKey.KStepGoal));
                DecimalScaleRulerViewSteps.initViewParam(stepGoal, 2000, maxStepValue, 100);
                tvGoalSteps.setText(stepGoal+"");
                break;
        }
    }
}
